package com.halachawiki.server;


import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.halachawiki.client.AuthorService;
import com.halachawiki.server.data.Author;
import com.halachawiki.server.data.User;
import com.halachawiki.server.data.UserContainer;
import com.halachawiki.shared.PermissionException;
import com.halachawiki.shared.PublicAuthor;

public class AuthorServiceImp extends RemoteServiceServlet implements
		AuthorService {

	private static final long serialVersionUID = 1L;
	
	@Override
	public Status newAuthor(PublicAuthor publicAuthor) throws PermissionException {
		PermissionManager.checkSiteModify(getThreadLocalRequest());
		User currentUser = UserContainer.getLogedInUser(getThreadLocalRequest());
//		User currentUser = UserContainer.getUserByEmail("admin@halachawiki.org");
		Author author = Author.getNewAuthor(currentUser, publicAuthor);
		author.commit();
		return Status.OK;
	}

	@Override
	public List<PublicAuthor> getAuthors() throws PermissionException {
		PermissionManager.checkSiteAccess(getThreadLocalRequest());
		User currentUser = UserContainer.getLogedInUser(getThreadLocalRequest());
//		User currentUser = UserContainer.getUserByEmail("admin@halachawiki.org");
		List<Author> authors = Author.getAllLatestVersions(currentUser);
		ArrayList<PublicAuthor> results = new ArrayList<PublicAuthor>();
		for (Author author : authors) {
			results.add(author.getPublicAuthor());
		}
		return results;
	}

}
